﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Diagnostics;
using System.IO;

namespace WpfImage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
        BitmapImage img;
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //absolute path
            img1.Source = new BitmapImage(new Uri(@"D:\wpfReview\wpfReview\WpfImage\r.png"));
            //relative path
            img2.Source = new BitmapImage(new Uri("r.png", UriKind.Relative));
            //web path and can get display process
            img = new BitmapImage(new Uri("http://172.30.52.75:9090/hdwiki/style/default/logo.gif"));
            img.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(img_DownloadProgress);
            img.DownloadCompleted += new EventHandler(img_DownloadCompleted);
            img.DownloadFailed += new EventHandler<ExceptionEventArgs>(img_DownloadFailed);
            //get inner path at package
            img4.Source = new BitmapImage(new Uri(@"pack://application:,,,/WpfImage;component/r.png"));
            //use Converter:monopolize read,when display can't delete
            img4.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(@"D:\wpfReview\wpfReview\WpfImage\r.png");
            //use Converter:non-monopolize read,when display can delete
            img4.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(File.ReadAllBytes(@"D:\wpfReview\wpfReview\WpfImage\r.png"));
        }

        void img_DownloadFailed(object sender, ExceptionEventArgs e)
        {
            Debug.WriteLine("Failure:" + e.ToString());
        }

        void img_DownloadCompleted(object sender, EventArgs e)
        {
            Debug.WriteLine("Finished");
            img3.Source = img;
        }

        void img_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            Debug.WriteLine(e.Progress);
        }
    }
}
